<?php


namespace seecms\lib\builder\control;

class RadioControl extends Control
{
    protected $type = 'radio';

    protected $options = [
        0 => '否',
        1 => '是'
    ];

    public function render(): string
    {
        $required = $this->required ? '<span class="layui-font-red">*</span>' : '';
        $readonly = $this->readonly ? 'readonly' : '';
        $disabled = $this->disabled ? 'disabled' : '';
        $description = $this->getDescription();
        $options = $this->getOptions();
        $option = '';
        foreach ($options as $key => $item) {

            $checked = '';
            if (!is_null($this->value) && $this->value == $key) {
                $checked = 'checked';
            }

            $option .= <<<EOF
            <input type="radio"
                   name="{$this->name}"
                   title="{$item}"
                   value="{$key}"
                   $checked
                   $readonly
                   $disabled
            >
EOF;
        }

        $display = 'block';
        $container = '<div class="layui-form-item">';
        if ($this->inline == 'inline') {
            $display = 'inline';
            $container = '<div class="layui-inline">';
        }

        return <<<EOF
$container
    <label class="layui-form-label">
       {$this->label}
       {$required}
    </label>
    <div class="layui-input-$display">
        $option
        $description
    </div>
</div>
EOF;

    }
}